package com.kgc.scd.listener;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kgc.scd.bean.ShopCart;
import com.kgc.scd.bean.ShopSendMessage;
import com.kgc.scd.constant.SendMessageMQConstant;
import com.kgc.scd.mapper.ShopSendMessageMapper;
import com.kgc.scd.util.RedisUtils;
import com.kgc.scd.util.Sample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Created On : 2022/11/10 0010.
 * <p>
 * Author : shiguangchuan
 * <p>
 * Description: 监听加仓信息，对需要接收短信提醒的用户发送短信
 */
@Slf4j
@Component
@RabbitListener(queues = SendMessageMQConstant.SEND_MASSAGE_RESULT_KH96) // 指定监听的消息队列
public class SendMessageToStockListener {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private ShopSendMessageMapper shopSendMessageMapper;

    /**
     * @author : shiguangchuan
     * @date   : 2022/11/17 0010
     * @param  : [seckillOrder]
     * @return : void
     * @description : 从加仓后生成的消息队列中，消费消息，发送短信提醒
     */
    @RabbitHandler
    public void sendMessageToUser(String goodsId){

        log.info("++++++ 加仓成功：{}，开始发送短信提醒 ++++++",goodsId);

        // 获取需要发送短信的用户号码
        Set<String> phoneList = redisTemplate.opsForZSet().range("NeedSendMessage" + ":" + goodsId,0,-1);
        // List<Object> phoneList = redisUtils.lGet(goodsId,0,-1);

        // 创建发送短信的方法对象
        Sample sample = new Sample();

        // 遍历登记过到货提醒的电话信息，需要短信提醒的电话信息
        phoneList.forEach(list -> {

            // 判断从未收到过短信提醒的电话，发送到货提醒
            if (redisTemplate.opsForZSet().score("NeedSendMessage" + ":" + goodsId,list) == 0) {

                try {
                    // 发送短信
                    if(sample.sendSms((String) list)) {

                        log.info("++++++ 加仓成功，向：{}，发送短信成功 ++++++", list);

                        // 发送成功后，改变电话的分值状态，避免下次加货后再次提醒
                        redisTemplate.opsForZSet().incrementScore("NeedSendMessage" + ":" + goodsId, list, 1);

                        return;
                    }

                    log.info("++++++ 加仓成功，向：{}，发送短信失败 ++++++", list);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        });
        // 删除redis中该商品的到货提醒的电话列表，避免后面加货重复提醒
        // redisUtils.del(goodsId);

        // TODO 对数据库中的信息做修改
        // 修改数据库中该商品需要提醒并被提醒的人的状态为1
        // mybatis-plus,创建一个查询对象
        QueryWrapper<ShopSendMessage> sendQueryWrapper = new QueryWrapper<>();
        // 添加条件语句
        sendQueryWrapper.eq("goods_id",goodsId).eq("is_send",0);

        // 查询出所有本次接收短信的信息，更改其状态和增加发送时间
        List<ShopSendMessage> shopSendMessageList = shopSendMessageMapper.selectList(sendQueryWrapper);

        // 遍历修改状态
        shopSendMessageList.forEach(sendList -> {

            // 更改状态和时间
            sendList.setIsSend(1);
            sendList.setModStatusTime(new Date());

            QueryWrapper<ShopSendMessage> sendQueryWrapper1 = new QueryWrapper<>();
            // 添加条件语句
            sendQueryWrapper1.eq("goods_id",goodsId).eq("is_send",0).eq("id",sendList.getId());

            // 执行修改
            shopSendMessageMapper.update(sendList,sendQueryWrapper1);

        });

        log.info("++++++ 加仓成功：{}，短信发送完成 ++++++",goodsId);


    }
}
